package com.ambow.restaurant.app.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.restaurant.app.po.Comment;
import com.ambow.restaurant.app.po.Food;
import com.ambow.restaurant.app.service.CommentServiceIMPL;
import com.ambow.restaurant.app.service.FoodServiceIMPL;
import com.ambow.restaurant.app.service.ICommentService;
import com.ambow.restaurant.app.service.IFoodService;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/app/comment.do")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    ICommentService comService = new CommentServiceIMPL();
    IFoodService foodService = new FoodServiceIMPL();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String method = request.getParameter("method");
		String foodIds = request.getParameter("foodId");
		Integer foodId = Integer.parseInt(foodIds);
		String userIds = request.getParameter("userId");
		
 		if(method.equals("index") && method!=null) {
			
			Food food = foodService.getFoodId(foodId);
			
			List<Comment> comment = comService.findById(foodId);
			request.setAttribute("comments", comment);
			request.setAttribute("food", food);
			
			request.getRequestDispatcher("/jsp/app/comment.jsp").forward(request, response);
		}else if(method.equals("submitTable")&&method!=null) {
			Integer userId = Integer.parseInt(userIds);
			String content = request.getParameter("contents");
			comService.comment(userId, foodId, content);
			response.sendRedirect(getServletContext().getContextPath()+"/app/index.do?method=findAllFood");
		}
	}

}
